package net.ak.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.ak.dao.CategoryDao;
import net.ak.dao.UserCategoryDao;
import net.ak.dao.UserDao;
import net.ak.model.Category;
import net.ak.model.User;
import net.ak.model.UserCategory;
import net.ak.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserCategoryDao userCategoryDao;
	
	@Autowired
	private CategoryDao categoryDao;
	
	/* (non-Javadoc)
	 * @see net.ak.service.UserService#listUsers()
	 */
	@Override
	public List<User> listUsers() {
		return userDao.listUsers();
	}

	/* (non-Javadoc)
	 * @see net.ak.service.UserService#getUserByEmail(java.lang.String)
	 */
	@Override
	public User getUserByEmail(String email) {
		// TODO Auto-generated method stub
		return userDao.getUserByEmail(email);
	}

	/* (non-Javadoc)
	 * @see net.ak.service.UserService#saveOrUpdate(net.ak.model.User)
	 */
	@Override
	public boolean saveOrUpdate(User user) {
		return userDao.saveOrUpdate(user);
	}

	/* (non-Javadoc)
	 * @see net.ak.service.UserService#addUserCategory(net.ak.model.User, net.ak.model.Category, java.lang.String)
	 */
	@Override
	public boolean addUserCategory(User user, Category category, String description, String cost) {
		UserCategory userCategory = new UserCategory();
		userCategory.setCategory(category);
		userCategory.setDescription(description);
		userCategory.setCost(cost);
		userCategory.setUser(user);
		return userCategoryDao.saveOrUpdate(userCategory);
	}

	/* (non-Javadoc)
	 * @see net.ak.service.UserService#listCategoriesByUser(net.ak.model.User)
	 */
	@Override
	public List<Category> listCategoriesByUser(User user) {
		List<UserCategory> userCategories = userCategoryDao.listUserCategoryByUser(user);
		return categoryDao.listCategoriesByCategoryIds(userCategories);
	}

	/* (non-Javadoc)
	 * @see net.ak.service.UserService#findUserCategory(net.ak.model.User, net.ak.model.Category)
	 */
	@Override
	public UserCategory findUserCategory(User user, Category category) {
		return null;
	}

	@Override
	public User getUserByEmailAndPassword(String email, String password) {
		return userDao.getUserByEmailAndPassword(email, password);
	}

	@Override
	public List<User> listUsersByCategory(Long catId) {
		List<UserCategory> userCategories = userCategoryDao.listUserCategoriesByCategoryId(catId);
		return userDao.listUsersByCategory(userCategories);
	}


}
